package cn.sumpay.servlet;

import cn.sumpay.model.AwardVoteMessage;
import cn.sumpay.model.JsonAwardVoteMessage;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import static cn.sumpay.utils.DataSourceUtil.getConnection;

/**
 * Created by Administrator on 2017/1/5.
 */
public class ChartData extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String awards_idstr = request.getParameter("awards_id");
        String awards_typestr = request.getParameter("awards_type");
        String active_idstr = request.getParameter("active_id");
        Connection connection = getConnection();
        try {
            List<AwardVoteMessage> voteMessages = new ArrayList<AwardVoteMessage>();
            if(awards_typestr.equals("1")) {
                String sql = "select * from vote_totaluser_message where active_id=" + active_idstr + " and awards_id = " + awards_idstr;
                PreparedStatement pst = connection.prepareStatement(sql);
                ResultSet rs = pst.executeQuery();
                while (rs.next()) {
                    Integer active_id = rs.getInt("active_id");
                    String adwards_name = rs.getString("awards_name");
                    Integer adwards_id = Integer.valueOf(awards_idstr);
                    Integer user_id = rs.getInt("user_id");
                    String user_name = rs.getString("user_name");
                    Integer vote_count = rs.getInt("vote_count");
                    voteMessages.add(new AwardVoteMessage(active_id, adwards_id, adwards_name, user_id, user_name, vote_count));

                }
            }else if(awards_typestr.equals("2")){
                String sql = "select * from VOTE_TOTALDEPART_MESSAGE where active_id="+active_idstr+" and awards_id="+awards_idstr;
                PreparedStatement pst = connection.prepareStatement(sql);
                ResultSet rs = pst.executeQuery();
                while(rs.next()){
                    Integer active_id = rs.getInt("active_id");
                    String adwards_name = rs.getString("awards_name");
                    Integer adwards_id = Integer.valueOf(awards_idstr);
                    Integer depart_id = rs.getInt("depart_id");
                    String depart_name = rs.getString("depart_name");
                    Integer vote_count = rs.getInt("vote_count");
                    voteMessages.add(new AwardVoteMessage(active_id,adwards_id,adwards_name,depart_id,depart_name,vote_count));
                }


            }
            //request.setAttribute("voteMessages", voteMessages);

            List list1 = new ArrayList();
            List list2 = new ArrayList();
            for(AwardVoteMessage awardVoteMessage : voteMessages){
                list1.add(awardVoteMessage.getUser_name());
                list2.add(awardVoteMessage.getVote_count());
            }
            JsonAwardVoteMessage jsondata = new JsonAwardVoteMessage(voteMessages.get(0).getActive_id(),voteMessages.get(0).getAdwards_id(),voteMessages.get(0).getAdwards_name(),list1,list2);


            JSONObject jsonObject = JSONObject.fromObject(jsondata);
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(jsonObject.toString());
            response.getWriter().close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
